package com.example.notificationservice.server;

import com.example.notificationservice.entity.*;
import com.example.notificationservice.service.NotificationService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author: dang
 * @Description: TODO
 * @Date: 2025/3/25 21:19 星期二
 */
@Service
public class KafkaConsumerService {

    @Autowired
    private NotificationService notificationService;
    private final ObjectMapper objectMapper = new ObjectMapper();
    @KafkaListener(topics = "user-messages", groupId = "user-group")
    public void consumeMessage(ConsumerRecord<String, String> record) throws JsonProcessingException {
        String key = record.key(); // 获取消息的键
        String value = record.value(); // 获取消息的值

        NotificationEntity notificationEntity = new NotificationEntity();

        if("下单成功".equals(key)){
            OrderMessage orderMessage = objectMapper.readValue(value, OrderMessage.class);

            notificationService.createNotification(notificationEntity);
        }



        System.out.println("Received message: Key = " + key + ", Value = " + value);
    }

}
